﻿using UnityEngine;
using System.Collections;

[System.Serializable]
public class Boundary
{
    public float xMin;
    public float xMax;
    public float yMin;
    public float yMax;
}
[System.Serializable]
public class Status
{
    public float Damage;
    public float Health;
}

public class PlayerController : MonoBehaviour {

    public float keySpeed;
    public float touchSpeed = 0.02f;
    public Boundary boundary;

    public Transform shotSpawn;
    public GameObject BoltPrefab;
    private float nextFire = 0.0f;
    public float fireRate = 0.5f;

    public Status status;
    public GameObject healthBarGUI;
    private HealthBarGUI healthBar;
    public GameObject Explosion;

    void Start(){
        healthBar = healthBarGUI.GetComponent<HealthBarGUI>();
    
    }

    void Update()
    {
        if (Time.time >= nextFire)
        {
          
            nextFire = Time.time + fireRate;
          GameObject bullet=   Instantiate(BoltPrefab,     //game Object
                       shotSpawn.position,
                       Quaternion.identity) as GameObject;
            
          bullet.GetComponent<BulletController>().damage = status.Damage;
           
        }

        if (status.Health <= 0)
        {
            Instantiate(Explosion, transform.position, Quaternion.identity);
            Destroy(gameObject);
        }
    }

    void FixedUpdate()
    {
        // Moving with Keyboard
        float moveHorizontal = Input.GetAxis("Horizontal");
        float moveVertical = Input.GetAxis("Vertical");

        Vector3 movement = new Vector3(moveHorizontal, moveVertical, 0.0f);
        rigidbody.velocity = movement * keySpeed;

        // Moving with touch
        //if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
        //{
        //    Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
        //    transform.Translate(touchDeltaPosition.x * touchSpeed, touchDeltaPosition.y * touchSpeed, 0);
        //}

        // Set range position
        rigidbody.position = new Vector3(
             Mathf.Clamp(transform.position.x, boundary.xMin, boundary.xMax),
             Mathf.Clamp(transform.position.y, boundary.yMin, boundary.yMax),
             0.0f);
        
    }

   public void Hurt(float damage)
    {
        status.Health -= damage;
        healthBar.guiTexture.pixelInset = new Rect(Screen.width / 50, Screen.height - Screen.height / 40,
            (healthBar.MaxWidth * (status.Health / 100)), Screen.height / 55);
    }


    float HurtTime;
    void OnTriggerStay(Collider other)
    {
        HurtTime+=Time.deltaTime;
        if (other.tag == Tags.enemy && HurtTime>=3)
        {
            EnemyController enemy;
            enemy = other.GetComponent<EnemyController>();

            if (enemy.Boss)
            {
                Hurt(enemy.status.Damage);
                HurtTime = 0;
            }
        }

    }

    void OnTriggerEnter(Collider other)
    {
        

        if (other.tag == Tags.enemy)
        {
            EnemyController enemy;
            enemy=other.GetComponent<EnemyController>();

            Hurt(enemy.status.Damage);
            if (!enemy.Boss)
            {
                enemy.status.Health = 0;
            }
        }

       
    }
}
